/**
* Copyright (c) 2012-2013 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
* Oracle and Java are trademarks or registered trademarks of Oracle and/or its
* affiliates. Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners. 
* See LICENSE.TXT for license information.
*/
package com.nokia.example.racer.views.components;

import com.nokia.example.racer.Main;
import com.nokia.example.racer.helpers.ImageLoader;
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/*
 * A clickable image item
 */
public abstract class Button
    extends Item {

    protected Image highlighted;  // image when clicked
    private boolean isHighlighted;

    public Button() {
    }

    /**
     * Initializes Button images.
     *
     * @param imgUrlUnpressed Image to be displayed when button is not pressed
     * @param imgUrlPressed Image to be displayed when button is pressed
     */
    public final void init(String imgUrlUnpressed, String imgUrlPressed) {
        ImageLoader loader = ImageLoader.getInstance();
        try {
            image = loader.loadImage(imgUrlUnpressed);
            highlighted = loader.loadImage(imgUrlPressed);
        }
        catch (IOException io) {
            // Nothing to do here.
        }
    }

    /**
     * Event when button is pressed
     */
    public abstract void onPress();

    /**
     * Event when button is released
     */
    public abstract void onRelease();

    /**
     * Renders the button image (highlight image if pressed).
     *
     * @param g Graphics object
     */
    public void render(Graphics g) {
        int anchor = Graphics.TOP | Graphics.LEFT;
        if (isHighlighted) {
            g.drawImage(highlighted, x, y, anchor);
        }
        else {
            g.drawImage(image, x, y, anchor);
        }
    }

    /**
     * Tells whether the given point is inside the button area.
     *
     * @param x x coordinate of the point
     * @param y y coordinate of the point
     * @return true if the given point is inside, false otherwise
     */
    public final boolean isInsideButton(int x, int y) {
        return getArea().isPointInside(x, y);
    }

    /**
     * When you add a button to canvas you must call this method from the
     * pointerPressed method in canvas class to make it work.
     * @see javax.microedition.lcdui.Canvas#pointerPressed(int x, int y)
     */
    public void pointerPressed(int x, int y) {
        if (isInsideButton(x, y)) {
            isHighlighted = true;
            onPress();
        }
        else {
            isHighlighted = false;
        }
    }

    /**
     * When you add a button to canvas you must call this method from the
     * pointerReleased method in canvas class to make it work.
     * @see javax.microedition.lcdui.Canvas#pointerReleased(int x, int y)
     */
    public void pointerReleased(int x, int y) {
        if (Main.gesturesSupported) {
            isHighlighted = false;
        }
        if (isInsideButton(x, y)) {
            onRelease();
        }
    }

    public void setActive() {
        isHighlighted = true;
    }

    public void setInactive() {
        isHighlighted = false;
    }

    public boolean isPressed() {
        return isHighlighted;
    }

    public Image getHighlighted() {
        return highlighted;
    }

    private final Rectangle getArea() {
        return new Rectangle(x, y, image.getWidth(), image.getHeight());
    }
}